package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SellingExpensesMapper;
import com.ruoyi.system.domain.SellingExpenses;
import com.ruoyi.system.service.ISellingExpensesService;

/**
 * 销售费用Service业务层处理
 * 
 * @author 王帝
 * @date 2022-08-02
 */
@Service
public class SellingExpensesServiceImpl implements ISellingExpensesService 
{
    @Autowired
    private SellingExpensesMapper sellingExpensesMapper;

    @Override
    public String importData(List<SellingExpenses> sellingExpensesList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(sellingExpensesList) || sellingExpensesList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<SellingExpenses> existList = selectSellingExpensesList(null);
        for (SellingExpenses importData : sellingExpensesList)
        {
            try {

                boolean userFlag = false;
                for (SellingExpenses entry : existList) {
                    if (entry.getPaymentItemNo().equals(importData.getPaymentItemNo())) {
                        userFlag = true;
                        break;
                    }
                }
                if (!userFlag) {
                    insertSellingExpenses(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getPaymentItemNo() + " 导入成功");
                } else if (isUpdateSupport) {
                    updateSellingExpenses(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getPaymentItemNo() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + importData.getPaymentItemNo() + " 已存在");
                }
            }catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + importData.getPaymentItemNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 查询销售费用
     * 
     * @param expId 销售费用主键
     * @return 销售费用
     */
    @Override

    public SellingExpenses selectSellingExpensesByExpId(Integer expId)
    {
        return sellingExpensesMapper.selectSellingExpensesByExpId(expId);
    }

    /**
     * 查询销售费用列表
     * 
     * @param sellingExpenses 销售费用
     * @return 销售费用
     */
    @Override
    @DataScope(deptAlias = "t",userAlias = "t")
    public List<SellingExpenses> selectSellingExpensesList(SellingExpenses sellingExpenses)
    {
        return sellingExpensesMapper.selectSellingExpensesList(sellingExpenses);
    }

    /**
     * 新增销售费用
     * 
     * @param sellingExpenses 销售费用
     * @return 结果
     */
    @Override
    public int insertSellingExpenses(SellingExpenses sellingExpenses)
    {
        return sellingExpensesMapper.insertSellingExpenses(sellingExpenses);
    }

    /**
     * 修改销售费用
     * 
     * @param sellingExpenses 销售费用
     * @return 结果
     */
    @Override
    public int updateSellingExpenses(SellingExpenses sellingExpenses)
    {
        return sellingExpensesMapper.updateSellingExpenses(sellingExpenses);
    }

    /**
     * 批量删除销售费用
     * 
     * @param expIds 需要删除的销售费用主键
     * @return 结果
     */
    @Override
    public int deleteSellingExpensesByExpIds(Integer[] expIds)
    {
        return sellingExpensesMapper.deleteSellingExpensesByExpIds(expIds);
    }

    /**
     * 删除销售费用信息
     * 
     * @param expId 销售费用主键
     * @return 结果
     */
    @Override
    public int deleteSellingExpensesByExpId(Integer expId)
    {
        return sellingExpensesMapper.deleteSellingExpensesByExpId(expId);
    }
}
